from django.db.models import Count

from home_design.models import InspirationVoting, Inspiration, Style
from home_design.serializers import InspirationListSerializer, StyleListSerializer
from home_design.utils import get_current_week_range
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status

from django.utils import timezone
from django.db.models import Count, Q

from users.models import CustomUser
from users.serializers import UserSerializer

"""作品数量最多的设计师"""


class TopInspirationUserView(APIView):
    def get(self,request):
        """获取作品数量最多的设计师"""
        top_inspiration_user = Inspiration.objects.values('designer').annotate(
            count=Count('designer')
        ).order_by('-count').first()
        if top_inspiration_user:
            user = CustomUser.objects.get(id=top_inspiration_user['designer'])
            serializer = UserSerializer(user)
            return Response(data={
                'designer':serializer.data,
                'inspiration_count':top_inspiration_user['count']
            }, status=status.HTTP_200_OK)
        return Response({"detail": "没有设计师"}, status=status.HTTP_404_NOT_FOUND)


class TopStyleUserView(APIView):
    def get(self,request):
        """获取作品数量最多的设计师"""
        top_style_user = Style.objects.values('designer').annotate(
            count=Count('designer')
        ).order_by('-count').first()
        if top_style_user:
            user = CustomUser.objects.get(id=top_style_user['designer'])
            serializer = UserSerializer(user)
            return Response(data={
                'designer':serializer.data,
                'style_count':top_style_user['count']
            }, status=status.HTTP_200_OK)
        return Response({"detail": "没有设计师"}, status=status.HTTP_404_NOT_FOUND)



class BestInspirationOfTheWeekView(APIView):
    def get(self, request):
        """获取本周投票最多的灵感"""
        one_week_ago = get_current_week_range()
        best_inspiration = Inspiration.objects.annotate(
            vote_count=Count('voted_by', filter=Q(voted_by__inspiration_voting_status=1))
        ).filter(
            voted_by__created_at__gte=one_week_ago,
            vote_count__gt=0
        ).order_by('-vote_count').first()

        if best_inspiration:
            serializer = InspirationListSerializer(best_inspiration, context={'request': request})
            return Response(serializer.data, status=status.HTTP_200_OK)
        return Response({"detail": "本周没有投票的灵感"}, status=status.HTTP_400_BAD_REQUEST)




class BestStyleOfTheWeekView(APIView):
    def get(self, request):
        """获取本周投票最多的单品"""
        one_week_ago = get_current_week_range()
        best_style = Style.objects.annotate(
            vote_count=Count('voted_by', filter=Q(voted_by__style_voting_status=1))
        ).filter(
            voted_by__created_at__gte=one_week_ago, # 过滤掉 7 天前的投票
            vote_count__gt=0 # 过滤掉 vote_count 为 0 的单品设计

        ).order_by('-vote_count').first()

        if best_style:
            serializer = StyleListSerializer(best_style, context={'request': request})
            return Response(serializer.data, status=status.HTTP_200_OK)
        return Response({"detail": "本周没有投票的单品设计"}, status=status.HTTP_404_NOT_FOUND)
